/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package candidate.beans;

import entities.Candidate;
import entities.CandidateSearchInterest;
import entities.Search;
import entities.SearchEducationalInfoRequerimentAndOperator;
import entities.SearchEducationalInfoRequerimentOrOperator;
import entities.SearchProfessionalInfoRequerimentAndOperator;
import entities.SearchProfessionalInfoRequerimentOrOperator;
import facades.CandidateFacade;
import facades.CandidateSearchInterestFacade;
import facades.SearchFacade;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.inject.Named;
import qualifiers.CreatedEntityEvent;
import qualifiers.LoggedIn;
import qualifiers.PreCreateEntityEvent;
import sqlgenerators.search.SearchSQLGenerator;
import sqlgenerators.search.SearchSqlGeneratorInterface;

/**
 *
 * @author Alvaro
 */
@Named
@SessionScoped
public class OffersCandidateBean implements Serializable {
    
    @Inject
    private SearchFacade searchFacade;
    @Inject
    private CandidateFacade candidateFacade;
    @Inject
    @LoggedIn
    private Candidate candidate;
    private SearchSqlGeneratorInterface sqlGenerator;
    @Inject
    private CandidateSearchInterestFacade candidateSearchInterestFacade;
    @Inject
    @CreatedEntityEvent
    private Event<CandidateSearchInterest> createdCandidateSearchInterestEvent;
    @Inject
    @PreCreateEntityEvent
    private Event<CandidateSearchInterest> preCreatedCandidateSearchInterestEvent;
    
    private Search selectedSearch;
    
    public OffersCandidateBean(){
        sqlGenerator=new SearchSQLGenerator();
    }
    
    public List<Search>getAllUserOffers(){
        List<Search>searchList= new ArrayList <Search>();
        List<Search>allPublicSearchs= searchFacade.getAllPublicSearchs();
        for(Search search:allPublicSearchs){
            String sql=sqlGenerator.getSqlFromSearch(search);
            if(searchFacade.candidateInASearch(candidate, sql)){
                searchList.add(search);
            }
        }
        return searchList;
    }
    
    
    public void candidateLikedASearch(Search search){
        if(search!=null && !exitsCandidateSearchInterest(search)){
            CandidateSearchInterest candidateSearchInterest = new CandidateSearchInterest();
            candidateSearchInterest.setDate(new Date());
            candidateSearchInterest.setCandidate(candidate);
            candidateSearchInterest.setSearch(search);
            preCreatedCandidateSearchInterestEvent.fire(candidateSearchInterest);
            candidateSearchInterestFacade.create(candidateSearchInterest);
            createdCandidateSearchInterestEvent.fire(candidateSearchInterest);
        }
    }
    
    public boolean exitsCandidateSearchInterest(Search search){
        return candidateSearchInterestFacade.exitsCandidateSearchInterest(candidate, search);
    }
    
    public List<Integer> getAllSectorOfSelectedSearch(){
        List<Integer>sectorIds=new ArrayList<Integer>();
        if(selectedSearch!=null){
            if(selectedSearch.getProfessionalInfoSector()!=null){
                sectorIds.add(selectedSearch.getProfessionalInfoSector().getIdSector());
                if(selectedSearch.getProfessionalInfoSector().getSearchEducationalInfoRequerimentAndOperatorList()!=null){
                    for(SearchEducationalInfoRequerimentAndOperator sector: selectedSearch.getProfessionalInfoSector().getSearchEducationalInfoRequerimentAndOperatorList()){
                        if(sector.getSector()!=null && !sectorIds.contains(sector.getSector().getIdSector())){
                            sectorIds.add(sector.getSector().getIdSector());
                        }
                    }
                }
                if(selectedSearch.getProfessionalInfoSector().getSearchEducationalInfoRequerimentOrOperatorList()!=null){
                    for(SearchEducationalInfoRequerimentOrOperator sector: selectedSearch.getProfessionalInfoSector().getSearchEducationalInfoRequerimentOrOperatorList()){
                        if(sector.getSector()!=null && !sectorIds.contains(sector.getSector().getIdSector())){
                            sectorIds.add(sector.getSector().getIdSector());
                        }
                    }
                }
                if(selectedSearch.getProfessionalInfoSector().getSearchProfessionalInfoRequerimentAndOperatorList()!=null){
                    for(SearchProfessionalInfoRequerimentAndOperator sector: selectedSearch.getProfessionalInfoSector().getSearchProfessionalInfoRequerimentAndOperatorList()){
                          if(sector.getSector()!=null && !sectorIds.contains(sector.getSector().getIdSector())){
                              sectorIds.add(sector.getSector().getIdSector());
                          }
                    }  
                }
                if(selectedSearch.getProfessionalInfoSector().getSearchProfessionalInfoRequerimentOrOperatorList()!=null){
                    for(SearchProfessionalInfoRequerimentOrOperator sector: selectedSearch.getProfessionalInfoSector().getSearchProfessionalInfoRequerimentOrOperatorList()){
                        if(sector.getSector()!=null && !sectorIds.contains(sector.getSector().getIdSector())){
                            sectorIds.add(sector.getSector().getIdSector());
                        }
                    }
                }
            }
        }
        return sectorIds;
    }

    /**
     * @return the selectedSearch
     */
    public Search getSelectedSearch() {
        return selectedSearch;
    }

    /**
     * @param selectedSearch the selectedSearch to set
     */
    public void setSelectedSearch(Search selectedSearch) {
        this.selectedSearch = selectedSearch;
    }
    
}
